﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;

namespace HZ.Supervise.Web.Secondphase.ReportHospital
{
    public partial class HospitalGoodsDetail : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
            if (!Page.IsPostBack)
            {     
                SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                if (!string.IsNullOrEmpty(sq.Mid)) //如果月份为空 参数有误
                {
                    BindList();
                }
                else
                {
                    Response.Write("<script>history.go(-1);<script>");
                }
            }
        }
        void BindList()
        {
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

            SuperviseQuery sqCopy = new SuperviseQuery();
            //临时
            sq.AreaID = null;
            sq.SortSource = null;
            HZ.Data.DAL_Helper.CopyModel(sq, sqCopy);
            if (!string.IsNullOrEmpty(txtSortInfo.Text.Trim()))
            {
                sqCopy.ProductName = DAL.DAL_Helper.Replace(txtSortInfo.Text.Trim());
            }
            if (!string.IsNullOrEmpty(TextBox2.Text.Trim()))
            {
                sqCopy.MedicineModelName = DAL.DAL_Helper.Replace(TextBox2.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtPack.Text.Trim()))
            {
                sqCopy.Pack = DAL.DAL_Helper.Replace(txtPack.Text.Trim());
            }

            if (!string.IsNullOrEmpty(txtUnit.Text.Trim()))
            {
                sqCopy.Unit = DAL.DAL_Helper.Replace(txtUnit.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtPurchaseName.Text.Trim()))
            {
                sqCopy.OrderName = DAL.DAL_Helper.Replace(this.txtPurchaseName.Text.Trim());
            }
            sqCopy.Append = Function.FormatPlanTime("PlanTime", sq.Mid);
            sqCopy.SortFields = "ProductName ASC";
            sqCopy.Mid = "";
            string strSortSource = Convert.ToString(sqCopy.SortSource);
            if (strSortSource == "-1")
            {
                strSortSource += "AND  SortSource in (0,1,2)";
            }
			sqCopy.Append += "  and OrderDetailState >0";
			
            int t = 0;
            HZ.Supervise.BLL.BLL_Statistics Bll_sta = (HZ.Supervise.BLL.BLL_Statistics)HZ.Supervise.BLLFactory.IBLL_Statistics();
            IList<HZ.Supervise.Model.Supervise> CompanyList;
            
            CompanyList = Bll_sta.GetTradeInfo(sqCopy, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
            DataTable dt = HZ.Data.DAL_Helper.GetDataTableFromModel<HZ.Supervise.Model.Supervise>(CompanyList);
            AspNetPager1.RecordCount = t;
            HZ.Supervise.Web.Function.ChangePSJE(dt, "DistributeCount", "Price");
            this.gvwDetail.DataSource = dt;
            this.gvwDetail.DataBind();

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
  
        }

        /// <summary>
        /// 合并表头
        /// create by张雄   2011.5.5
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void gvwDetail_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:
                    //第一行表头
                    TableCellCollection tc = e.Row.Cells;
                    tc.Clear();
                    tc.Add(new TableHeaderCell());
                    tc[0].Attributes.Add("colspan", "18"); //跨Column
                    tc[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    tc[0].Text = ForMatDate(sq.Mid) + " " + sq.HospitalName + "</th></tr><tr>";
                    
                    //第2行表头
                    tc.Add(new TableHeaderCell());
                    tc[1].Attributes.Add("bgcolor", "Khaki");
                    tc[1].Text = "序号";

                    tc.Add(new TableHeaderCell());
                    tc[2].Attributes.Add("bgcolor", "Khaki");
                    tc[2].Text = "采购数量";

                    tc.Add(new TableHeaderCell());
                    tc[3].Attributes.Add("bgcolor", "Khaki");
                    tc[3].Text = "采购总金额";

                    tc.Add(new TableHeaderCell());
                    tc[4].Attributes.Add("bgcolor", "Khaki");
                    tc[4].Text = "配送数量";

                    tc.Add(new TableHeaderCell());
                    tc[5].Attributes.Add("bgcolor", "Khaki");
                    tc[5].Text = "配送总金额";

                    tc.Add(new TableHeaderCell());
                    tc[6].Attributes.Add("bgcolor", "Khaki");
                    tc[6].Text = "入库数量";

                    tc.Add(new TableHeaderCell());
                    tc[7].Attributes.Add("bgcolor", "Khaki");
                    tc[7].Text = "入库总金额";

                    tc.Add(new TableHeaderCell());
                    tc[8].Attributes.Add("bgcolor", "Khaki");
                    tc[8].Text = "采购单名称";

                    tc.Add(new TableHeaderCell());
                    tc[9].Attributes.Add("bgcolor", "Khaki");
                    tc[9].Text = "开始时间";

                    tc.Add(new TableHeaderCell());
                    tc[10].Attributes.Add("bgcolor", "Khaki");
                    tc[10].Text = "截至时间";

                    tc.Add(new TableHeaderCell());
                    tc[11].Attributes.Add("bgcolor", "Khaki");
                    tc[11].Text = "通用名";

                    tc.Add(new TableHeaderCell());
                    tc[12].Attributes.Add("bgcolor", "Khaki");
                    tc[12].Text = "剂型";

                  
                    tc.Add(new TableHeaderCell());
                    tc[13].Attributes.Add("bgcolor", "Khaki");
                    tc[13].Text = "采购规格";

                    tc.Add(new TableHeaderCell());
                    tc[14].Attributes.Add("bgcolor", "Khaki");
                    tc[14].Text = "采购单位";

                    tc.Add(new TableHeaderCell());
                    tc[15].Attributes.Add("bgcolor", "Khaki");
                    tc[15].Text = "采购单价（元）";

                    tc.Add(new TableHeaderCell());
                    tc[16].Attributes.Add("bgcolor", "Khaki");
                    tc[16].Text = "生产企业";

                    tc.Add(new TableHeaderCell());
                    tc[17].Attributes.Add("bgcolor", "Khaki");
                    tc[17].Text = "配送企业";

                    tc.Add(new TableHeaderCell());
                    tc[18].Attributes.Add("bgcolor", "Khaki");
                    tc[18].Text = "采购医院";
                    break;
            }
        }

        /// <summary>
        /// 格式化日期
        /// create by 张雄  2011.5.5
        /// </summary>
        /// <param name="str">日期参数</param>
        /// <returns>格式化后的日期</returns>
        private string ForMatDate(string str)
        {
            string YearStr = String.Empty;
            string MonStr = String.Empty;
            if (!string.IsNullOrEmpty(str))
            {
                if (str.Length == 6)
                {
                    YearStr = str.Substring(0, 4) + "年";   //取得年
                    MonStr = str.Substring(4, 2);    //取得月
                    if (MonStr.Substring(0, 1) == "0")  //判断如果
                    {
                        MonStr = MonStr.Substring(1, 1) + "月";
                    }
                    else
                    {
                        MonStr += "月";
                    }
                }
            }
            return str = YearStr + MonStr;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

            BindList();

        }

        /// <summary>
        /// 导出
        /// by张雄  2011.5.23
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {

            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

            SuperviseQuery sqCopy = new SuperviseQuery();

            sq.SortSource = null;
            HZ.Data.DAL_Helper.CopyModel(sq, sqCopy);
            if (txtSortInfo.Text.Trim() != "")
            {
                sqCopy.ProductName = DAL.DAL_Helper.Replace(txtSortInfo.Text.Trim());
            }
            if (TextBox2.Text.Trim() !="")
            {
                sqCopy.MedicineModelName = DAL.DAL_Helper.Replace(TextBox2.Text.Trim());
            }

            if (txtPack.Text.Trim() != "")
            {
                sqCopy.Pack = DAL.DAL_Helper.Replace(txtPack.Text.Trim());
            }
            if (txtUnit.Text.Trim() != "")
            {
                sqCopy.Unit = DAL.DAL_Helper.Replace(txtUnit.Text.Trim());
            }
            if (this.txtPurchaseName.Text.Trim()!="")
            {
                sqCopy.OrderName = DAL.DAL_Helper.Replace(this.txtPurchaseName.Text.Trim());
            }
            sqCopy.Append = Function.FormatPlanTime("PlanTime", sq.Mid);
			sqCopy.Append += " and OrderDetailState >0";
            sqCopy.SortFields = "ProductName ASC";
            sqCopy.Mid = "";
            string strSortSource = Convert.ToString(sqCopy.SortSource);
            if (strSortSource != "-1")
            {
                strSortSource += "AND  (SortSource = 3 or SortSource = 5 or SortSource = 6)";

            }


            int t = 0;
            HZ.Supervise.BLL.BLL_Statistics Bll_sta = (HZ.Supervise.BLL.BLL_Statistics)HZ.Supervise.BLLFactory.IBLL_Statistics();
            IList<HZ.Supervise.Model.Supervise> CompanyList;
            CompanyList = Bll_sta.GetTradeInfo(sqCopy, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
            DataTable dt = HZ.Data.DAL_Helper.GetDataTableFromModel<HZ.Supervise.Model.Supervise>(CompanyList);
            HZ.Supervise.Web.Function.ChangePSJE(dt, "DistributeCount", "Price");
            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwDetail);
            
        }
    }
}
